package cn.ztf.zshop.storage.service;

import cn.ztf.zshop.storage.config.MinioConfig;
import io.minio.*;
import io.minio.errors.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 *
 * 命名规范, 存储软件+是否存储桶+Storage
 *
 * minio 桶存储
 */

@Slf4j
@Service
@ConditionalOnBean(MinioClient.class)
@RequiredArgsConstructor
public class MinioBucketStorage implements BucketStorage{

    protected final MinioClient minioClient;

    protected final MinioConfig minioConfig;

    @Override
    public void createBucket(String bucketName) {
        try {
            MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder()
                    .bucket(bucketName)
                    .build();
            minioClient.makeBucket(makeBucketArgs);
        } catch (Exception e) {
            log.error("创建桶失败", e);
        }
    }

    public void createBucket(MakeBucketArgs args){
        try {
            minioClient.makeBucket(args);
        }catch (Exception e){
            log.error("创建桶失败", e);
        }
    }


    @Override
    public void create(InputStream inputStream, String fileName, String bucketName) {
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);
        } catch (Exception e) {
            log.error("上传文件失败", e);
        }
    }

    @Override
    public void download(String fileName, String bucketName, OutputStream outputStream) {
        try {
            DownloadObjectArgs downloadObjectArgs = DownloadObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .build();
            minioClient.downloadObject(downloadObjectArgs);
            download(downloadObjectArgs);
        }catch (Exception e){
            log.error("下载文件失败", e);
        }
    }

    public void download(DownloadObjectArgs downloadObjectArgs){
        try {
            minioClient.downloadObject(downloadObjectArgs);
        }catch (Exception e){
            log.error("下载文件失败", e);
        }
    }

    @Override
    public void create(InputStream inputStream, String fileName) {
       this.create(inputStream, fileName, minioConfig.getBucketName());
    }

    @Override
    public GetObjectResponse getObject(String fileName) {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .build();
        try {
            GetObjectResponse object = minioClient.getObject(getObjectArgs);
            return object;
        }catch (Exception e){
            log.error("获取文件失败", e);
        }
        return null;
    }

    @Override
    public void download(String fileName, OutputStream outputStream) {
        this.download(fileName, minioConfig.getBucketName(), outputStream);
    }


}
